﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetJson()
        {
          
            var Method = Request["Method"].ToString();
            var parmer = Request["parmer"].ToString();
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            var timespan = Convert.ToInt64(ts.TotalSeconds).ToString();
            //JObject json = JObject.Parse(parmer);
            var parameters = new Dictionary<string, dynamic>();
            parameters.Add("timespan", timespan);
            //foreach (var jItem in json)
            //{
            //    parameters.Add(jItem.Key, jItem.Value);
            //}
            //json.Add("timespan", timespan);
            var client = new WebClient();

            //若以后涉及授权校验接口可启用
            //    client.Headers.Add("sLoginName", user.sLoginName);
            //    client.Headers.Add("sPassWord", user.sPassWord);
            //    client.Headers.Add("sUserId", user.sUserId);
            //不指定Headers
            //var postData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(parmer));
            var postData = Encoding.UTF8.GetBytes(parmer);
            //指定header 参数化
            var dic = new Dictionary<string, object>
            {
                {"sign", "sss"},
                {"name", "a"}

            };
            // string postData = string.Join("&", dic.Select(x => x.Key + "=" + x.Value));
            // client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            // byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            var responseData = client.UploadData("http://192.168.17.245:8078/api/Home/GetPwd", Method, postData
                );
            client.Dispose();
            var str = Encoding.UTF8.GetString(responseData);
            return Content(str);

        }
    }
}